using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Enttiies;
using Bll;
namespace SimpleThreeTier
{
    public partial class Form1 : Form
    {
        CategoryBll cateBll = new CategoryBll();
        public Form1()
        {
            InitializeComponent();
        }

        private void AddItem(CategoryInfo cate)
        {
            ListViewItem lvItem = new ListViewItem(cate.CategoryID.ToString());
            lvItem.SubItems.Add(cate.CategoryName);
            lvItem.SubItems.Add(cate.Description);
            lvItem.Tag = cate;
            lstCategories.Items.Add(lvItem);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // get all recoards from Categories table
            IList<CategoryInfo> categories = cateBll.GetAll();
            // display the list of categories to ListView lstCategories
            foreach (CategoryInfo cate in categories)
            {
                AddItem(cate);
            }
        }

        private void lstCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItems.Count > 0)
            {
                // lay lai doi tuong CategoryInfo
                CategoryInfo cate = lstCategories.SelectedItems[0].Tag as CategoryInfo;
                if (cate != null)
                {
                    txtCategoryID.Text = cate.CategoryID.ToString();
                    txtCategoryName.Text = cate.CategoryName;
                    rtxtDescription.Text = cate.Description;
                    picture.Image = cate.Picture;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // bo qua doan kiem tra tinh hop le
            CategoryInfo cate = new CategoryInfo();
            cate.CategoryName = txtCategoryName.Text;
            cate.Description = rtxtDescription.Text;
            cate.Picture = picture.Image;
            if (cateBll.Insert(cate))
            {
                // insert vao csdl thanh cong thi hien thi len ListView
                AddItem(cate);
            }
            else
                MessageBox.Show("Error");

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Image files (*.jpg)|*.jpg|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                picture.ImageLocation = openFileDialog1.FileName;
            }

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItems.Count > 0)
            {
                // lay lai ListViewItem dang duoc chon
                ListViewItem lvItem = lstCategories.SelectedItems[0];
                CategoryInfo cate = lvItem.Tag as CategoryInfo;
                // Lay thong tin Category nguoi su dung da hieu chinh
                CategoryInfo nCate = new CategoryInfo();
                nCate.CategoryID = cate.CategoryID;
                nCate.CategoryName = txtCategoryName.Text;
                nCate.Description = rtxtDescription.Text;
                nCate.Picture = picture.Image;

                if (cateBll.Update(nCate))
                {
                    // Cap nhat thanh cong, sua lai thong tin tren ListView
                    lvItem.SubItems[1].Text = nCate.CategoryName;
                    lvItem.SubItems[2].Text = nCate.Description;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstCategories.SelectedItems.Count > 0)
            {
                // lay lai ListViewItem dang duoc chon
                ListViewItem lvItem = lstCategories.SelectedItems[0];
                CategoryInfo cate = lvItem.Tag as CategoryInfo;


                if (cateBll.Delete(cate))
                {
                    // Xoa thanh cong, xoa thong tin khoi  ListView
                    lstCategories.Items.Remove(lvItem);
                }
                else
                    MessageBox.Show("Delete Error");
            }
        }
    }
}